<?php

namespace app\course\event\listeners\comment;

use app\course\logic\base\Course;
use app\course\logic\comment\Comment as CommentLogic;
use app\course\model\comment\admin\Comment as AdminComment;

/**
 * 课程点评评分事件
 */
class CommentScore
{
    public static function handle(CommentLogic $data)
    {
        $comments  = app(AdminComment::class)->where('course_id', $data->course_id)->select()->toArray();
        $score_num = count($comments);
        $score_sum = array_sum(array_column($comments, 'star'));
        $score_avg = ceil($score_sum / $score_num);
        //
        $course = app(Course::class)->findOrEmpty($data->course_id);
        //
        $course->score_nums    = $score_num;
        $course->score_sums    = $score_sum;
        $course->score_average = $score_avg;
        $course->save();
    }
}
